package com.aternity.vbd.collect;

import com.aternity.vbd.collect.feedback.FeedbackData;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author Daniel Orr
 */
@Repository
public class ApplicationDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public ApplicationsData getApplicationsAndActivities() {

        final String sql = "SELECT \n" +
                "c.pretty_name application, \n" +
                "a.ID, \n" +
                "a.pretty_name activity \n" +
                "FROM \n" +
                "container c \n" +
                "JOIN monitor m ON c.id = m.container_id \n" +
                "JOIN activity a ON m.id = a.monitor_id \n" +
                "JOIN activity_monitored_attr att ON a.id = att.activity_id\n" +
                "WHERE \n" +
                "   c.deleted+m.deleted+a.deleted+att.deleted = 0 \n" +
                "   and att.monitored_attr_type_id=17";

        final ApplicationsData applicationsData = new ApplicationsData();

        jdbcTemplate.query(sql, new Object[]{}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                String app = rs.getString(1);
                Integer activityId = rs.getInt(2);
                String activity = rs.getString(3);
                applicationsData.add(app, activityId, activity);
            }
        });
        return applicationsData;
    }
}
